
//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js');

Page({
  data: {
    userInfo: {},
    videoUrl: '9.94 RXz:/ 复制打开抖音，看看【重庆诺严律师事务所的作品】虽然法律对老人有一定保护，但可别掉以轻心，情节严重... https://v.douyin.com/DkXcc3P/',
  },

  onLoad: function () {

  },

  onShow() {
    // 如果剪切板内有内容则尝试自动填充
    wx.getClipboardData({
      success: res => {
        var str = res.data.trim()
        if (this.regUrl(str)) {
          wx.showModal({
            title: '检测到剪切板有视频地址，是否自动填入？',
            success: res => {
              if (res.confirm) {
                this.setData({
                  videoUrl: str
                })
              }
            }
          })
        }
      }
    })
  },

  // 清空输入框
  inputClear: function () {
    this.setData({
      videoUrl: ''
    })
  },

  // 视频地址匹配是否合法
  regUrl: function (t) {
    return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(t)
  },

  submit: function () {


    var msg = "输入不合法";
    if (!this.regUrl(this.data.videoUrl)) {
      //检查输入是否合法
      wx.showToast({
        title: msg,
        icon: 'none'
      })
      return;
    }




    var num;
    var today = util.formatDate(new Date(), '');
    var lastParseDate = wx.getStorageSync('lastParseDate');
    if (lastParseDate != today) {
      wx.setStorageSync('lastParseDate', today);
      wx.setStorageSync('dailyFreeParseNum', app.globalData.defaultDailyFreeParseNum);
      num = app.globalData.defaultDailyFreeParseNum;
    } else {
      num = wx.getStorageSync('dailyFreeParseNum');
    }
    this.parseVideo();


  },

  // 视频解析
  parseVideo: function () {
    wx.showLoading({
      title: '解析中',
      mask: true
    })
    app.apiRequest({
      url: '/TKApi/parseVideoEx',
      method: 'GET',
      data: {
        link: this.data.videoUrl,
        token: wx.getStorageSync('token')
      },
      success: res => {
        console.log(res)
        wx.hideLoading()


        if (res.data.success) {
          var noWaterUrl = encodeURIComponent(res.data.data.noWaterUrl);
          var imageUrl = encodeURIComponent(res.data.data.imageUrl);
          var preview = res.data.data.preView;
          wx.setStorageSync('dailyFreeParseNum', wx.getStorageSync('dailyFreeParseNum') - 1);
          wx.navigateTo({
            url: "../video/video?url=" + noWaterUrl + '&image=' + imageUrl + '&preview=' + preview,
          })
        }
        else {

          wx.showToast({
            title: '用的人太多了，请稍后重试',
            icon: 'none'
          })
        }

      }
    })
  }
})
